package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import pojo.Device;

import utils.db.impl.DeviceFetcher;

/**
 * Servlet implementation class InformationDeviceServlet
 */
@WebServlet("/InformationDeviceServlet")
public class InformationDeviceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		if (request.getParameter("deviceId") != null) {
			PrintWriter out = response.getWriter();
			int deviceId = Integer.parseInt(request.getParameter("deviceId"));
			DeviceFetcher d = new DeviceFetcher();
			Device device = d.getDevice(deviceId);
			JSONObject jsonReturn = new JSONObject();
			jsonReturn.put("clientId", String.valueOf(device.getClientId()));
			jsonReturn.put("servicemanId",  String.valueOf(device.getServicemanId()));
			jsonReturn.put("type",  String.valueOf(device.getType()));
			jsonReturn.put("desc",  String.valueOf(device.getDescription()));
		
			out.print(jsonReturn);
			out.flush();
		}
	}

}
